package com.leetcode.LeetCode精选TOP面试题;

import java.util.HashSet;
import java.util.Set;

/**
 * @author 覃国强
 * @date 2022-07-07 16:24
 */
public class 题库_0202_快乐数 {

  public static void main(String[] args) {
    System.out.println(new Solution().isHappy(7));
  }

  static
  class Solution {
    public boolean isHappy(int n) {
      Set<Integer> set = new HashSet<>();
      while (n != 1 && !set.contains(n)) {
        set.add(n);
        n = getNext(n);
      }
      return !set.contains(n);
    }

    private int getNext(int n) {
      int answer = 0;
      while (n != 0) {
        int num = n % 10;
        answer += num * num;
        n /= 10;
      }
      return answer;
    }
  }

}
